# encoding: utf-8

import numpy
from . import base_device

class pv_device(base_device.base_device):
    def __init__(self, req):
        super(pv_device, self).__init__()

        #
        self.P = None

        self.Q = None

        self.Qmax = None

        self.Qmin = None


    ''' value is in kW and default time is 1 hour'''
    def apply_charge(self, value):
        if value > 0:
            self.soc = self.soc + value * 0.95 - 0.01 * self.soc
        else:
            self.soc = self.soc + value / 0.95 - 0.01 * self.soc

        if self.soc > self.soc_max:
            self.soc = self.soc_max
        if self.soc < self.soc_min:
            self.soc = self.soc_min

    def apply_charge_series(self, charge_series):
        for i in range(len(charge_series)):
            self.apply_charge(charge_series[i])

    ''' value is in kW and default time is 1 hour'''
    def get_charge_limit(self):
        return (self.soc_max - self.soc + 0.01 * self.soc)/0.95

    def get_discharge_limit(self):
        return (self.soc_min - self.soc + 0.01 * self.soc) * 0.95

    def check_charge_bound(self, charge):
        if charge > self.Pmax_charge:
        #if charge > self.Pmax_charge or charge < self.Pmax_discharge:
            return False
        else:
            return True

    def check_soc_bound(self, charge_series):
        # Assume that time step is 1 hour and values are in kW
        battery_soc = [0] * (len(charge_series) + 1)
        battery_soc[0] = self.soc

        for j in range(0, len(charge_series)):
            if charge_series[j] > 0:
                battery_soc[j + 1] = battery_soc[j] + charge_series[j] * 0.95 - 0.01 * battery_soc[j]
            else:
                battery_soc[j + 1] = battery_soc[j] + charge_series[j] / 0.95 - 0.01 * battery_soc[j]

        #if any([x > soc_max or x < 0 for x in battery_soc]):
        for j in range(0,  len(charge_series)):
            if (battery_soc[j + 1] > self.soc_max) or (battery_soc[j + 1] < self.soc_min):
                return False

        return True

    def getQDecreaseLimit(self):
        return self.Qmin - self.Q

    def getQIncreaseLimit(self):
        return self.Qmax - self.Q
